/**
 * Copyright &copy; 2012-2016 <a href="https://git.oschina.net/zdw2016/webbase5">Webbase</a> All rights reserved.
 */
package com.foreveross.webbase.bookdemo.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.foreveross.webbase.common.config.Global;
import com.foreveross.webbase.common.persistence.Page;
import com.foreveross.webbase.common.web.BaseController;
import com.foreveross.webbase.common.utils.StringUtils;
import com.foreveross.webbase.bookdemo.entity.SysBook;
import com.foreveross.webbase.bookdemo.service.SysBookService;

/**
 * 图书管理Controller
 * @author yangwanli
 * @version 2017-03-08
 */
@Controller
@RequestMapping(value = "${adminPath}/bookdemo/sysBook")
public class SysBookController extends BaseController {

	@Autowired
	private SysBookService sysBookService;
	
	@RequiresPermissions("bookdemo:sysBook:view")
	@RequestMapping(value = {"list", ""})
	public String list(SysBook sysBook) {
		Page<SysBook> page = sysBookService.findPage(new Page<SysBook>(request(), response()), sysBook); 
		attr("page", page);
		return "bookdemo/sysBookList";
	}

	@RequiresPermissions("bookdemo:sysBook:view")
	@RequestMapping("form")
	public String form(String id) {
		if(StringUtils.isNotEmpty(id)) {
			SysBook sysBook=sysBookService.get(id);
			attr("sysBook", sysBook);
		} else {
			attr("sysBook", new SysBook());
		}
		return "bookdemo/sysBookForm";
	}

	@RequiresPermissions("bookdemo:sysBook:edit")
	@RequestMapping("save")
	public String save(SysBook sysBook, RedirectAttributes redirectAttributes) {
		if (!beanValidator(redirectAttributes, sysBook)){
			return form(sysBook.getId());
		}
		sysBookService.save(sysBook);
		addMessage(redirectAttributes, "保存图书成功");
		return "redirect:"+Global.getAdminPath()+"/bookdemo/sysBook/?repage";
	}
	
	@RequiresPermissions("bookdemo:sysBook:edit")
	@RequestMapping("delete")
	public String delete(@RequestParam String id, RedirectAttributes redirectAttributes) {
		sysBookService.delete(id);
		addMessage(redirectAttributes, "删除图书成功");
		return "redirect:"+Global.getAdminPath()+"/bookdemo/sysBook/?repage";
	}

}